package com.zxx.flowerstore.utils.aspect;

import com.alibaba.fastjson.JSON;
import com.zxx.flowerstore.service.LogService;
import com.zxx.flowerstore.utils.common.LogInfo;
import com.zxx.flowerstore.utils.utils.EmptyUtil;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LogAspect
 * @Author Admin
 * @Date 2021/3/20 12:13
 * @Description LogAspect
 * @Version 1.0
 */
@Aspect
@Component
public class LogAspect {

    private static Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(* com.zxx.flowerstore.controller..*.*(..))")
    public void log() {
    }

    @Autowired
    LogService logService;

    /**
     * 进入Controller获取参数
     *
     * @param joinPoint
     */
    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        LogInfo logInfo = new LogInfo();
        logger.info("==============================start==============================");

        //url
        logger.info("url=" + request.getRequestURL());

        logInfo.setUrl(StringUtils.isEmpty(request.getRequestURL())?null:request.getRequestURL().toString());

        //methon
        logger.info("method=" + request.getMethod());

        logInfo.setMethod(StringUtils.isEmpty(request.getMethod())?null: request.getMethod());


        //ip
        logger.info("ip=" + request.getRemoteHost());


        logInfo.setIp(StringUtils.isEmpty(request.getRemoteHost())?null: request.getRemoteHost());


        //类方法
        logger.info("class_method=" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

        logInfo.setClassMethod(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

        //参数
        Object[] objs = joinPoint.getArgs();
        List args = new ArrayList();
        for (Object obj : objs) {
            if ((obj instanceof HttpServletRequest) || (obj instanceof HttpServletResponse) || obj instanceof BindingResult) {
                continue;
            }
            args.add(obj);
        }
        if (!EmptyUtil.isEmpty(args)) {
            logger.info("args=" + JSON.toJSONString(args));
            logInfo.setRequestParam(JSON.toJSONString(args));
        }

        request.setAttribute("msg",logInfo);

    }

    @After("log()")
    public void doAfter() {

    }

    @AfterReturning(returning = "object", pointcut = "log()")
    public void adAfterReturning(Object object) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        logger.info("resopnse=" + JSON.toJSONString(object));
        logger.info("==============================end==============================");
        if(request.getAttribute("msg")!=null){
            LogInfo jcosMsg = (LogInfo) request.getAttribute("msg");
            jcosMsg.setResponse(JSON.toJSONString(object));
            logService.saveLog(jcosMsg);
        }
    }


}
